package com.sunsharing.common;


import com.sunsharing.common.config.Config;
import com.sunsharing.common.serialize.support.nativejava.NativeJavaSerialization;
import com.sunsharing.common.utils.StringUtils;
import org.apache.log4j.Logger;

import java.io.ByteArrayInputStream;

/**
 * 报文创建Factory
 * User: criss
 * Date: 13-4-2
 * Time: 上午11:23
 * To change this template use File | Settings | File Templates.
 */
public class ContextCreate {
    static Logger logger = Logger.getLogger(ContextCreate.class);

    public static Context createHeartContext()
    {
        Msg m = new Msg();
        m.msg = Constant.HEART_MSG;
        Context context = new Context(Constant.MSG_TYPE_HEART,m);
        return context;
    }

    public static Context createConnectContext(String url)
    {
        Msg m = new Msg();
        m.msg = url;
        Context context = new Context(Constant.MSG_TYPE_CONNECT,m);
        return context;
    }

    public static Context createMailBoxStopContext()
    {
        Msg m = new Msg();
        m.msg = "";
        Context context = new Context(Constant.MSG_TYPE_STOP,m);
        return context;
    }

    public static Context createRequestContext(String msg)
    {
        Msg m = new Msg();
        m.msg = msg;
        m.initRequest();
        Context context = new Context(Constant.MSG_TYPE_REQUEST,m);
        context.taskId = StringUtils.genUUID();
        return context;
    }

    public static Context createResultContext(String msg,Context reqContext)
    {
        Msg m = new Msg();
        m.msg = msg;
        m.initRequest();
        Context context = new Context(Constant.MSG_TYPE_RESULT,m);
        context.taskId =reqContext.taskId;
        context.beginNode = reqContext.beginNode;
        context.setApiNode(reqContext.apiNode);
        context.history = reqContext.history;
        return context;
    }
    public static Context createErrorContext(String msg,Context reqContext)
    {
        Msg m = new Msg();
        m.msg = msg;
        //m.initRequest();
        Context context = new Context(Constant.MSG_TYPE_RESULT,m);
        context.taskId =reqContext.taskId;
        context.beginNode = reqContext.beginNode;
        context.setApiNode(reqContext.apiNode);
        context.history = reqContext.history;
        return context;
    }

    public static Context createContextByByteArray(byte[] bytes)
    {
        NativeJavaSerialization out = new NativeJavaSerialization();
        try
        {
            Context o2 = (Context)out.deserialize(new ByteArrayInputStream(bytes)).readObject();
            return o2;
        }catch (Exception e)
        {
            logger.error("反序列化Context出错",e);
            throw new RuntimeException("反序列化Context出错");
        }
    }
    public static Context createContextByByteArray(byte[] bytes,int start,int end)
    {
        NativeJavaSerialization out = new NativeJavaSerialization();
        try
        {
            Context o2 = (Context)out.deserialize(new ByteArrayInputStream(bytes,start,end)).readObject();
            return o2;
        }catch (Exception e)
        {
            logger.error("反序列化Context出错",e);
            throw new RuntimeException("反序列化Context出错");
        }
    }

}
